<template>
  <div class="flexContent">
    <div class="searchBox">
      <!-- 用户管理 -->
      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="状态">
                  <el-select v-model="sForm.status" style="width: 95%" clearable placeholder="请选择">
                    <el-option value="" label="全部" />
                    <el-option value="0" label="使用中" />
                    <el-option value="1" label="已暂停" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="申请时间">
                  <el-date-picker v-model="date" clearable type="daterange" unlink-panels range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 95%" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button class="searchBtn" icon="el-icon-search" @click="searchFn">搜索</el-button>
            <el-button class="resetBtn" icon="el-icon-refresh-right" @click="resetFn">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="userName" title="用户名" min-width="150"/>
          <vxe-column field="status" title="状态" width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="primary" plain size="small">使用中</el-tag>
              <el-tag v-if="row.status === '1'" disabled type="info" plain size="small">已禁用</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="createTime" title="注册时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.createTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column v-if="$showFn(['shopuser_edit'])" fixed="right" width="200" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" v-has="'shopuser_edit'" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="useredit(row)">编辑</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>

    <!-- 新增/修改弹窗 -->
    <el-dialog :close-on-click-modal="false" title="编辑" width="30%" :visible.sync="dialogFormVisible" @close="btnClose">
      <el-form ref="addForm" label-width="80px">
        <el-form-item label="用户">
          <span>{{ info.userName }}</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="info.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button class="celBtn" @click="btnClose">取 消</el-button>
        <el-button class="addBtn" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, updateUserStatus } from "@/api/shopManagement";
export default {
  name: "ShopUserList",
  data() {
    return {
      sForm: {
        status: '',
        openTime: '',
        endTime: ''
      },
      dialogFormVisible: false,
      info: {
        id: '',
        userName: '',
        status: '',
      },
      date: [],
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      shopUserList: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      if (!this.isSearch) {
        this.sForm = {
          status: '',
          openTime: '',
          endTime: ''
        };
      }
      const res = await getUserList({
        ...this.page,
        ...this.sForm
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.shopUserList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("shopUserList", this.shopUserList);
        const $table = this.$refs.xTable1
        $table.loadData(this.shopUserList);
      } else {
        // this.$message.error(res.message);
      }
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getUserList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getUserList();
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.sForm.openTime = this.date?.length ? this.date[0] : "";
      this.sForm.endTime = this.date?.length ? this.date[1] : "";
      this.isSearch = true;
      this.getUserList();
    },
    resetFn() {
      this.sForm = {
        status: '',
        openTime: '',
        endTime: ''
      };
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.getUserList();
    },
    btnClose() {
      this.dialogFormVisible = false
      this.info = {
        id: '',
        userName: '',
        status: '',
      }
    },
    useredit(row) {
      this.info.id = row.id
      this.info.userName = row.userName
      this.info.status = row.status
      this.dialogFormVisible = true
    },
    async btnOk() {
      const res = await updateUserStatus({
        id: this.info.id,
        status: this.info.status
      })
      if (res.code === 200) {
        this.$message.success('修改成功')
        this.dialogFormVisible = false
        this.getUserList()
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.txtBox {
  padding-left: 20px;
  margin-bottom: 30px;

}

.btnBOX {
  text-align: center;
}
</style>
